package com.ocom.common.utils;

import com.ocom.common.vo.ugcapi.StaticCodePayVo;

import java.util.Locale;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2023/3/7
 **/

public class StaticCodePayUtil {

	private static String key = "J6RLZ44BHRQ4ENG4";

	/**
	 *
	 * @param type  1 机器号  2 码id
	 * @param comId
	 * @param codeId
	 * @return
	 */
	public static String getCode(Integer type,Long comId,String code){
		String typeStr = NumConvertUtil.IntLegth(type,2);
		String comStr = NumConvertUtil.IntLegth(comId.intValue(),8);
		String codeLenStr  = NumConvertUtil.IntLegth(code.length(),3);
		String staticCode  = "XAK" +"_"+comStr+"_"+typeStr+"_"+codeLenStr+"_"+code+"_" ;
		String md5s = MD5.MD5Encode((staticCode+key).toUpperCase(Locale.ROOT),"UTF-8");
		return (staticCode+md5s).toUpperCase(Locale.ROOT);
	}

	public static String getCode2(Long type,Long comId,Integer id){
		String typeStr = NumConvertUtil.IntLegth(type.intValue(),2);
		String comStr = NumConvertUtil.IntLegth(comId.intValue(),8);
		String staticCode  = "XAK" +"_"+comStr+"_"+typeStr+"_"+id+"_" ;
		String md5s = MD5.MD5Encode((staticCode+key).toUpperCase(Locale.ROOT),"UTF-8");
		return (staticCode+md5s).toUpperCase(Locale.ROOT);
	}

	public static String getCode3(Long comId,Long pId){
		String comStr = NumConvertUtil.IntLegth(comId.intValue(),8);
		String pIdStr = NumConvertUtil.IntLegth(pId.intValue(),8);
		String staticCode  = "XAK" +"_"+comStr+"_"+pIdStr+"_" ;
		String md5s = MD5.MD5Encode((staticCode+key).toUpperCase(Locale.ROOT),"UTF-8");
		return (staticCode+md5s).toUpperCase(Locale.ROOT);
	}


	public static StaticCodePayVo getCodeInfo(String code){
		if(code.length()<52 || !code.startsWith("XAK")){
			return null;
		}
		String md5s = MD5.MD5Encode(code.substring(0,code.length()-32)+key,"UTF-8");
		if(!code.substring(code.length()-32).equals(md5s.toUpperCase(Locale.ROOT))){
			return null;
		}
		Integer type = Integer.valueOf(code.substring(13,15));
		Integer comId = Integer.valueOf(code.substring(4,12));

		Integer codeLen =NumConvertUtil.HexStringToInt(code.substring(16,19));
		String codeStr = code.substring(20,20+codeLen);

		StaticCodePayVo staticCodePayVo =new StaticCodePayVo();
		staticCodePayVo.setType(type);
		staticCodePayVo.setComId(comId.longValue());

		if(CommonUtil.isNull(codeStr)){
			return staticCodePayVo;
		}

		if(type==1){
			staticCodePayVo.setDeviceNo(codeStr);
			return staticCodePayVo;
		}
		if(type==2){
			staticCodePayVo.setCodeId(Long.valueOf(codeStr));
			return staticCodePayVo;
		}
		return null;
	}



}
